-
Notifications
You must be signed in to change notification settings - Fork 13.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(dao): Condense delete/bulk-delete operations #24466
chore(dao): Condense delete/bulk-delete operations #24466
Conversation
@@ -30,19 +27,6 @@ | |||
class AnnotationDAO(BaseDAO): | |||
model_cls = Annotation | |||
|
|||
@staticmethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copypasta except the BaseDAO
is defined as a classmethod
as opposed to a staticmethod
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments are very helpful while reviewing. Thank you.
@@ -67,21 +51,6 @@ def validate_update_uniqueness( | |||
class AnnotationLayerDAO(BaseDAO): | |||
model_cls = AnnotationLayer | |||
|
|||
@staticmethod | |||
def bulk_delete( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copypasta except the BaseDAO
is defined as a classmethod
as opposed to a staticmethod
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup!
from superset.models.core import CssTemplate | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CssTemplateDAO(BaseDAO): | ||
model_cls = CssTemplate | ||
|
||
@staticmethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copypasta except the BaseDAO
is defined as a classmethod
as opposed to a staticmethod
.
Codecov Report
@@ Coverage Diff @@
## master #24466 +/- ##
===========================================
- Coverage 69.08% 58.33% -10.75%
===========================================
Files 1906 1906
Lines 74156 74107 -49
Branches 8161 8165 +4
===========================================
- Hits 51232 43234 -7998
- Misses 20805 28750 +7945
- Partials 2119 2123 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 267 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
2208e64
to
51dc939
Compare
try: | ||
annotation = AnnotationDAO.delete(self._model) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to return the object we're deleting.
5b99d4b
to
8b63521
Compare
@@ -1364,7 +1364,7 @@ def delete_embedded(self, dashboard: Dashboard) -> Response: | |||
$ref: '#/components/responses/500' | |||
""" | |||
for embedded in dashboard.embedded: | |||
DashboardDAO.delete(embedded) | |||
EmbeddedDashboardDAO.delete(embedded) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm somewhat surprised that #24465 didn't pick this up.
02e8b79
to
cbb621d
Compare
@@ -36,8 +36,10 @@ def __init__(self, model_ids: list[int]): | |||
|
|||
def run(self) -> None: | |||
self.validate() | |||
assert self._models |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is temporary. It should be cleaned up when we refactor the commands and enforce that validate
doesn't augment any properties.
superset/daos/dataset.py
Outdated
""" | ||
Deletes a Dataset column | ||
""" | ||
return cls.delete(model, commit=commit) | ||
return DatasetColumnDAO.delete(model, commit=commit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more accurate, i.e., the DatasetColumnDAO
rather than the DatasetDAO
should be deleting the column.
try: | ||
if not self._model: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DRY. The validate
method already raises a DatasetMetricNotFoundError
if the metric is not found.
superset/daos/base.py
Outdated
@classmethod | ||
def delete(cls, model: T, commit: bool = True) -> T: | ||
def delete(cls, objects: T | list[T], commit: bool = True) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
items
seems to be a more accurate/consistent than models
, i.e., we're deleting database items/instances as opposed to models.
ee29647
to
3620631
Compare
def bulk_delete(models: Optional[list[SqlaTable]], commit: bool = True) -> None: | ||
item_ids = [model.id for model in models] if models else [] | ||
@classmethod | ||
def delete( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bulk_deletion
logic differs from the delete
logic in the sense that the former first deletes the relationships (which in actuality may not be explicitly required) before deleting the dataset, whereas the later only deletes the datasets (which in theory should delete all the records with an explicit relationship).
3620631
to
aa543ae
Compare
aa543ae
to
14664d8
Compare
14664d8
to
6aaeed0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -30,19 +27,6 @@ | |||
class AnnotationDAO(BaseDAO): | |||
model_cls = Annotation | |||
|
|||
@staticmethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments are very helpful while reviewing. Thank you.
@@ -67,21 +51,6 @@ def validate_update_uniqueness( | |||
class AnnotationLayerDAO(BaseDAO): | |||
model_cls = AnnotationLayer | |||
|
|||
@staticmethod | |||
def bulk_delete( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup!
Thank you for the nice cleanup @john-bodley! |
Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> (cherry picked from commit 7409289)
SUMMARY
Per SIP-92 Proposal for restructuring the Python code base #24331 organized all the DAOs to be housed within a shared folder—the result of which highlighted numerous inconsistencies, repetition, and inefficiencies.
This PR (one of many smaller bitesized PRs) condenses the single (
delete
) and bulk deletion (bulk_delete
) logic into a single (delete
) method which removes a bunch of copypasta and helps to standardize the code.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
ADDITIONAL INFORMATION